package com.intranet.fund.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.intranet.fund.domain.Applicant;
import com.intranet.fund.domain.Email;
import com.intranet.fund.service.ApplicantService;
import com.intranet.fund.service.EmailService;
import com.intranet.fund.util.EmailUtil;
import com.water.dao.Page;
import com.water.domain.DataTableParameter;
import com.water.web.Tool;

@Controller
public class ApplicantController {

	@Autowired
	private ApplicantService applicantService;
	@Autowired
	private EmailService emailService;

	@RequestMapping("/getAllApplicant")
	public void getAllApplicant(HttpServletResponse response,
			DataTableParameter param) {

		// 搜索条件
		String search = param.getSSearch();
		// 查询结果集
		Page page = applicantService.getAllApplicant(param.getPageNo(), param
				.getIDisplayLength(), search);
		List<Applicant> applicants = page.getData();
		List<Object> list = new ArrayList<Object>();
		for (Applicant a : applicants) {
			list.add(new String[] {
					"<input name=\"checkbox\" type=\"checkbox\" class=\"checkboxes\" value=\""
							+ a.getId().toString() + "\" />",
					a.getFullName(),
					a.getPhone(), a.getGender(),
					a.getCityName(),a.getTypesHousing(),a.getTypeBusiness(),a.getCardSalary()});
		}
		// 封装页面参数
		String aaData = JSONArray.fromObject(list).toString();
		JSONObject json = new JSONObject();
		json.put(DataTableParameter.SECHO, param.getSEcho());
		json.put(DataTableParameter.ITOTALRECORDS, page.getTotalCount());// 过滤前总记录数
		json.put(DataTableParameter.ITOTALDISPALYRECORDS, page.getTotalCount());// 过滤后总记录数
		json.put(DataTableParameter.AADATA, aaData);
		Tool.writeByAction(json.toString(), response);
	}

	@RequestMapping("/loadApplicant")
	public ModelAndView loadApplicant(HttpServletRequest request,
			@RequestParam(value = "id", required = true)
			int id, @RequestParam(value = "type", required = true)
			String type) {
		ModelAndView mav = new ModelAndView();
		Applicant applicant = applicantService.getApplicantById(id);
		mav.addObject("applicant", applicant);
		if (type.equals("edit")) {
			mav.setViewName("/sys/applicant/editApplicant");
		} else {
			mav.setViewName("/sys/applicant/showApplicant");
		}
		return mav;
	}

	@RequestMapping("/addApplicant")
	public ModelAndView addApplicant(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/fund/applicant/addApplicant");
		return mav;
	}

	@RequestMapping("/saveApplicant")
	public ModelAndView saveApplicant(HttpServletResponse response, Applicant applicant) {
		ModelAndView mav = new ModelAndView();
		if (applicant.getId() == null) {
			Applicant a = new Applicant();
			a.setFullName(applicant.getFullName());
			a.setGender(applicant.getGender());
			a.setPhone(applicant.getPhone());
			a.setCityName(applicant.getCityName());
			applicantService.saveApplicant(a);
			
			//邮件提醒管理员
			List<Email> list = emailService.getAllEmailByStatus("Y");
			String address = "";
			for(Email e:list){
				address += e.getAccount()+";";
			}
			EmailUtil cn = new EmailUtil();
			cn.setAddress(address);
			cn.send("亲爱的管理员，您好！ 又有一位新客户【"+applicant.getFullName()+"】注册了我们的公积金网。请您赶快联系喽！");
		} else {
			Applicant a = applicantService.getApplicantById(applicant.getId());
			applicantService.updateApplicant(a);
		}
		mav.setViewName("/fund/applicant/saveApplicantSuccess");
		return mav;
	}

	@RequestMapping("/deleteApplicant")
	public void deleteApplicant(HttpServletResponse response,
			@RequestParam(value = "ids", required = false)
			String ids) {
		String msg = "";
		String[] id = ids.split(",");
		for (int i = 0; i < id.length; i++) {
			Applicant a = applicantService.getApplicantById(Integer
					.valueOf(id[i]));
			msg = applicantService.deleteApplicant(a);
		}
		Tool.writeByAction(msg, response);
	}
}
